﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Atlandiz.Data;

namespace Atlandiz.EnvanterVeZimmetYonetimi
{
    public partial class FormPersonel : DevExpress.XtraEditors.XtraForm
    {
        //MsSqlAccessLayer SQL = new MsSqlAccessLayer(".\\SQLExpress","TeknoEnva",true);

        public int KayID;

        public FormPersonel()
        {
            InitializeComponent();
        }

        public void KayitGetir()
        {
            //textEdit1.Text = SQL.GetDataCell("Select Ad from Personel where pID=" + KG);
            //textEdit2.Text = SQL.GetDataCell("Select Soyad from Personel where pID=" + KG);
            //textEdit3.Text = SQL.GetDataCell("Select Gorev from Personel where pID=" + KG);
            //textEdit4.Text = SQL.GetDataCell("Select DepAdi from Departman where dID=(Select dID from Personel where pID=" + KG + ")");
            //textEdit5.Text = SQL.GetDataCell("Select dID from Departman where dID=(Select dID from Personel where pID=" + KG + ")");

            DataTable dt = Army.SQL().GetDataTable("Select Ad,Soyad,Gorev from Personel where pID=" + KayID);
            _txtAdString.Text = dt.Rows[0][0].ToString();
            _txtSoyadString.Text = dt.Rows[0][1].ToString();
            _txtGorevUnvanString.Text = dt.Rows[0][2].ToString();
            DataTable dt1 = Army.SQL().GetDataTable("Select DepAdi,dID from Departman where dID=(Select dID from Personel where pID=" + KayID + ")"); 
            _txtDepartanString.Text = dt1.Rows[0][0].ToString();
            _txtDepartmanInt.Text = dt1.Rows[0][1].ToString();

        }

       
    
        private void Kullanici_Load(object sender, EventArgs e)
        {
            if (KayID != 0)

            {
                KayitGetir();
            }

           
        }
        public void formuTemizle()
        {
            _txtAdString.Text = "";
            _txtSoyadString.Text = "";
            _txtGorevUnvanString.Text = "";
            _txtDepartanString.Text = "";
            _txtDepartmanInt.Text = "";
            _txtAdString.Focus();
        }

        private void _btnSec_Click(object sender, EventArgs e)
        {
            ListeDepartman dl = new ListeDepartman();
            dl.ForSelection = true;
            dl.ShowDialog();

            if (Army.EventConfirm == true)
            {
                _txtDepartmanInt.Text = Army.SecilenID.ToString();
                _txtDepartanString.Text = Army.SQL().GetDataCell("Select DepAdi from Departman where dID=" + _txtDepartmanInt.Text);
            }
        }

        private void _btnFormuTemizle_Click(object sender, EventArgs e)
        {
            formuTemizle();
        }

        private void _btnKaydet_Click(object sender, EventArgs e)
        {
            if (KayID == 0)
            {
                if (_txtAdString.Text == "")
                {
                    MessageBox.Show("Lütfen, Personel Adı giriniz...");
                }
                else if (_txtSoyadString.Text == "")
                {
                    MessageBox.Show("Lütfen, Personel Soyadı giriniz...");
                }
                else if (_txtDepartanString.Text == "")
                {
                    MessageBox.Show("Lütfen, Personelin bağlı olduğu departmanı seçiniz...");
                }
                else
                {
                    Army.SQL().cmd("Insert Personel(Ad,Soyad,Gorev,dID) values('" + _txtAdString.Text.ToString().Replace("'", "''") + "','" + _txtSoyadString.Text.ToString().Replace("'", "''") + "','" + _txtGorevUnvanString.Text.ToString().Replace("'", "''") + "','" + _txtDepartmanInt.Text + "')");
                    formuTemizle();
                }
            }
            else
            {
                if (_txtAdString.Text == "")
                {
                    MessageBox.Show("Lütfen, Personel Adı giriniz...");
                }
                else if (_txtSoyadString.Text == "")
                {
                    MessageBox.Show("Lütfen, Personel Soyadı giriniz...");
                }
                else
                {
                    Army.SQL().cmd("Update Personel Set Ad='" + _txtAdString.Text.ToString().Replace("'", "''") + "', Soyad='" + _txtSoyadString.Text.ToString().Replace("'", "''") + "', Gorev='" + _txtGorevUnvanString.Text.ToString().Replace("'", "''") + "', dID='" + _txtDepartmanInt.Text + "' where pID=" + KayID);
                    MessageBox.Show("Kayıt {" + KayID + "} Güncellendi.");
                    this.Close();
                }

            }
        }

        private void _btnKapat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void _btnPersonelListesi_Click(object sender, EventArgs e)
        {
            ListePersonel kl = new ListePersonel();
            kl.ShowDialog();
        }
    }
}